<?php

namespace App\Controller\Wap;

use App\Entity\MoneyWithdrawAccount;
use App\Entity\ShopGoods;
use App\Entity\UserLikesProduct;
use App\Model\MoneyLogModel;
use App\Model\MoneyModel;
use App\Model\MoneyWaitModel;
use App\Model\ShopGoodsModel;
use App\Model\UserLikesProductModel;
use App\Base\BaseController4User;
use App\Model\LazyCommon\CCM;
use App\Model\LazyCommon\PlatConfigModel;
use App\Model\LazyCommon\RulesModel;
use Model\UserBundle\Entity\User;
use App\Model\LazyCommon\UserModel;
use App\Model\LazyCommon\UserGroupsModel;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\HttpFoundation\Request;


/**
 * @route("/wap")
 */
class MoneyController extends BaseController4User
{

    /**
     * @route("/money/all",name="UserAppMoneyLogAll")
     */
    public function UserAppMoneyLogAll()
    {
        if(!$flag = $this->getParamRequired("flag")){
            $flag = 999;
        }
        if ($flag == -1) {$flag = 0;}
        
        $uid = $this->needToBeUser();
        $MoneyModel = new MoneyModel($this->getDoctrine());
        $Money = $MoneyModel->getUserMoney($uid);


        //mone log
        $MoneyLogModel = new MoneyLogModel($this->getDoctrine());
        $logs = $MoneyLogModel->getUserLog($uid,$flag);
        $logs = $MoneyLogModel->listFormat($logs);


        $this->data["money"] = $Money;
        $this->data["logs"] = $logs;
        $this->data["flag"] = $flag;
        switch ($flag) {
            case 0:
                $flagName = "冻结积分";
                break;
            case 999:
                $flagName = "全部";
                break;
            case 1:
                $flagName = "红包收益";
                break;
            case 2:
                $flagName = "积分奖励";
                break;
            case 3:
                $flagName = "地区代理奖励";
                break;
            default:
                $flagName = "";
        }
        $this->data["flagName"] = $flagName;

        return $this->render("App/Wap/money_log_all.php.twig",$this->data);
    }

    /**
     * @route("/money/json",name="UserAppMoneyLogJson")
     */
    public function UserAppMoneyLogJson()
    {
        $uid = $this->needToBeUser();
        if(!$flag = $this->getParamRequired("flag")){return $this->json_out(0,"参数缺失");}
        if(!$page = $this->getParamRequired("page")){$page = 0;}
        if ($flag == -1) {$flag = 0;}

        //money log
        $MoneyLogModel = new MoneyLogModel($this->getDoctrine());
        $logs = $MoneyLogModel->getUserLog($uid,$flag,$page);

        $logs = $MoneyLogModel->listFormat($logs);

        $this->data["logs"] = $logs;
        return $this->json_out(1,"数据获取成功",$logs);
    }



    /**
     * @route("/money/wait",name="UserAppMoneyLogWait")
     */
    public function UserAppMoneyLogWait()
    {
        if(!$flag = $this->getParamRequired("flag")){
            $flag = 999;
        }
        if ($flag == -1) {$flag = 0;}

        $uid = $this->needToBeUser();
        $MoneyModel = new MoneyWaitModel($this->getDoctrine());
        $Money = $MoneyModel->getSum4User($uid);
        //mone log

        $logs = $MoneyModel->getList4User($uid);
        $this->data["money"] = $Money;
        $this->data["logs"] = $logs;
        $this->data["flag"] = $flag;
        switch ($flag) {
            case 0:
                $flagName = "冻结积分";
                break;
            case 999:
                $flagName = "全部";
                break;
            case 1:
                $flagName = "红包收益";
                break;
            case 2:
                $flagName = "积分奖励";
                break;
            case 3:
                $flagName = "地区代理奖励";
                break;
            default:
                $flagName = "";
        }
        $this->data["flagName"] = $flagName;

        return $this->render("App/Wap/money_log_wait.php.twig",$this->data);
    }



    /**
     * 积分兑换记录
     * @route("/money/log/out",name="UserAppMoneyLogOut")
     */
    public function UserAppMoneyLogOut()
    {
        $uid = $this->needToBeUser();
        if(!$page = $this->getParamRequired("page")){$page = 0;}
        $MoneyModel = new MoneyModel($this->getDoctrine());
        $money = $MoneyModel->getUserMoney($uid);
        $this->data["money"] = $money;

        //获取用户积分提现记录
        $Model = new MoneyModel($this->getDoctrine());
        $Model->getWithdrawListLog($uid,$page,100);

        return $this->render("App/Wap/money_log_out.php.twig",$this->data);
    }

    /**
     * 积分兑换主页
     * @route("/money/bind/card/index",name="UserAppMoneyBindCardIndex")
     */
    public function UserAppMoneyBindCardIndex()
    {
        $uid = $this->needToBeUser();
        if(!$page = $this->getParamRequired("page")){$page = 0;}
        $MoneyModel = new MoneyModel($this->getDoctrine());
        $money = $MoneyModel->getUserMoney($uid);
        $this->data["money"] = $money;

        //获取用户积分提现记录
        $Model = new MoneyModel($this->getDoctrine());
        $Model->getWithdrawListLog($uid,$page,100);

        //获取用户等级，根据等级决定积分兑换的显示情况
        $UserModel = new UserModel($this->getDoctrine());
        $UserGroupsModel = new UserGroupsModel($this->getDoctrine());
        $RulesModel = new RulesModel($this->getDoctrine());
        $RulesModel->getByK("money_level_up_vip");

        $user = $UserModel->getUserById($uid);
        $level = $user->getGroups();
        $levelMoneyVip= "0";
        $levelNameVip= "";
        if ($level < 5) {
            $levelNameVip = $UserGroupsModel->getGroupNameByGroup(5);
            $Rule = $RulesModel->getByK("money_level_up_vip");
            $levelMoneyVip = (int)$Rule->getV();
        }
        $this->data["levelMoneyVip"] = $levelMoneyVip;
        $this->data["levelNameVip"] = $levelNameVip;

        $levelMoneyAgent = "0";
        $levelNameAgent = "";
        if ($level < 6) {
            $levelNameAgent = $UserGroupsModel->getGroupNameByGroup(6);
            $Rule = $RulesModel->getByK("money_level_up_agent");
            $levelMoneyAgent = (int)$Rule->getV();
        }
        $this->data["levelMoneyAgent"] = $levelMoneyAgent;
        $this->data["levelNameAgent"] = $levelNameAgent;


        return $this->render("App/Wap/money_bind_card_index.php.twig",$this->data);
    }

    /**
     * 绑卡页面
     * @route("/money/bind/card",name="UserAppMoneyBindCard")
     */
    public function UserAppMoneyBindCard(Request $request)
    {
        $uid = $this->needToBeUser();
        if ($request->getMethod() == "POST") {
            if(!$account = $this->getParamRequired("account")){return $this->json_out(0,"参数缺失01");}
            if(!$name = $this->getParamRequired("name")){return $this->json_out(0,"参数缺失02");}
            if(!$bank = $this->getParamRequired("bank")){return $this->json_out(0,"参数缺失03");}
            if(!$kaihuhang = $this->getParamRequired("kaihuhang")){return $this->json_out(0,"参数缺失04");}

            $MoneyModel = new MoneyModel($this->getDoctrine());
            $Entity = $MoneyModel->addWithdrawAccount($uid,$account,$name,"$bank-".$kaihuhang);
            return $this->json_out(1,"添加成功");
        }else{
            $uid = $this->needToBeUser();
            if(!$page = $this->getParamRequired("page")){$page = 0;}
            $MoneyModel = new MoneyModel($this->getDoctrine());
            $money = $MoneyModel->getUserMoney($uid);
            $this->data["money"] = $money;
            $Entity = $MoneyModel->getWithdrawAccount($uid);


            if (!$Entity) {
                $Entity = new MoneyWithdrawAccount();
                $Entity = $this->toArray($Entity);
                $Entity["bank"] = "";
                $Entity["params"] = "";
                $Entity["kaihuhang"] = "";
            }else{
                $param = $Entity->getParams();
                $paramArray = mb_split("-",$param);
                $Entity = $this->toArray($Entity);
                $Entity["bank"] =$paramArray[0];
                $Entity["params"] =$paramArray[1];
                $Entity["kaihuhang"] =$paramArray[1];
            }

            //获取用户积分提现记录
            $Model = new MoneyModel($this->getDoctrine());
            $Model->getWithdrawListLog($uid,$page,100);
        }
        $this->data["acc"] = json_encode($Entity);

        return $this->render("App/Wap/money_bind_card.php.twig",$this->data);

    }


    /**
     * @route("/money/withdraw/do",name="UserAppMoneyWithdrawDo")
     */
    public function UserAppMoneyWithdrawDo()
    {
        $uid = $this->needToBeUser();
        $MoneyModel = new MoneyModel($this->getDoctrine());
        //先检查是否设置了收款账号
        if (!$account = $MoneyModel->getWithdrawAccount($uid)) {
            return $this->json_out(0,"请先绑定卡号信息");
        }

        if(!$levelUp = $this->getParamRequired("levelUp")){
            //积分直接兑换的情况

            if(!$money = $this->getParamRequired("money")){return $this->json_out(0,"参数缺失");}
            //检测最小值
            $RulesModel = new RulesModel($this->getDoctrine());
            $Rule = $RulesModel->getByK("money_withdraw_min");
            $min = $Rule->getV();
            if ($money < $min) {
                return $this->json_out(0,"单次提现最少".$min);
            }

            //检测对大值
            $Rule = $RulesModel->getByK("money_withdraw_max");
            $max = $Rule->getV();
            if ($money > $max) {
                return $this->json_out(0,"单次提现不能超过".$max);
            }

            //检测当日提现是否达到最大次数
            $Rule = $RulesModel->getByK("money_withdraw_limit");
            $num = $Rule->getV();
            $list = $MoneyModel->getWithdrawListLog($uid,0,$num);
            if (count($list) == $num) {
                if ($list[$num-1]->getAddTime() > $this->getDayStart()) {
                    return $this->json_out(0,"每日提现限制".$num."次");
                }
            }

            $money = floor($money * 100)/100;

            //检查用户是否有足够的金额
            $userMoney = $MoneyModel->getUserMoney( $uid );
            $curMoney = $userMoney->getCur();
            if ($money > $curMoney) {
                return $this->json_out(0,"余额不足，刷新或者查看积分记录");
            }


            $error = $MoneyModel->withdraw($uid,$money);

            if ($error) {
                return $this->json_out(0,$error);
            }else{
                //添加提现记录
                $MoneyModel->addWithdrawLog($uid,$money,$this->createOrderSn(),1,json_encode($_SERVER["HTTP_USER_AGENT"]));
                $userMoney = $MoneyModel->getUserMoney($uid);
                return $this->json_out(1,"审核已提交",$userMoney->getCur());
            }

        }else{
            if(!$money = $this->getParamRequired("money")){return $this->json_out(0,"参数缺失");}
            //积分兑换身份的情况
            //获取用户等级，根据等级决定积分兑换的显示情况
            $UserModel = new UserModel($this->getDoctrine());
            $UserGroupsModel = new UserGroupsModel($this->getDoctrine());
            $RulesModel = new RulesModel($this->getDoctrine());
            $RulesModel->getByK("money_level_up_vip");

            $user = $UserModel->getUserById($uid);
            $level = $user->getGroups();
//            $levelMoney= "";
//            $levelName= "";
            $levelName = $UserGroupsModel->getGroupNameByGroup($level + 1);
            $Rule = $RulesModel->getByK("money_level_up_vip");
            $levelMoney = (int)$Rule->getV();

            if ($money !=  $levelMoney) {

                $levelName = $UserGroupsModel->getGroupNameByGroup($level + 1);
                $Rule = $RulesModel->getByK("money_level_up_agent");
                $levelMoney = (int)$Rule->getV();
                if ($money !=  $levelMoney) {
                    //升级金额不符
                    return $this->json_out(0,"升级金额异常请联系管理员");
                }
            }

            $this->data["levelMoney"] = $levelMoney;
            $this->data["levelName"] = $levelName;


            //检查用户是否有足够的金额
            $userMoney = $MoneyModel->getUserMoney( $uid );
            $curMoney = $userMoney->getCur();
            if ($levelMoney > $curMoney) {
                return $this->json_out(0,"余额不足，刷新或者查看积分记录");
            }


            $error = $MoneyModel->withdraw($uid,$levelMoney);
            $message = "用户申请升级成为 【 $levelName 】扣去 $levelMoney 积分";
            if ($error) {
                return $this->json_out(0,$error);
            }else{
                //添加提现记录
                $MoneyModel->addWithdrawLog($uid,$levelMoney,$this->createOrderSn(),1,$message);
                $userMoney = $MoneyModel->getUserMoney($uid);
                return $this->json_out(1,"审核已提交",$userMoney->getCur());
            }

        }



    }



    /**
     * @route("/money/withdraw/log",name="UserAppMoneyWithdrawIndex")
     */
    public function UserAppMoneyWithdrawIndex()
    {
        $uid = $this->needToBeUser();
        if(!$page = $this->getParamRequired("page")){$page = 0;}
        $MoneyModel = new MoneyModel($this->getDoctrine());
        $money = $MoneyModel->getUserMoney($uid);
        $data["money"] = $this->toArray($money);

        //获取用户积分提现记录
        $Model = new MoneyModel($this->getDoctrine());
        $list = $Model->getWithdrawListLog($uid,$page,100);
        foreach ($list as &$one) {
            $one->setAddTime(date(YYMMDDHHIISS,$one->getAddTime()));
            $this->toArray($one);
        }
        $data["list"] = $list;

        return $this->json_out(1,"数据获取成功",$data);
    }

    /**
     * @route("/money/card/info",name="UserAppMoneyWithdrawCardInfo")
     */
    public function UserAppMoneyWithdrawCardInfo()
    {
        $uid = $this->needToBeUser();
        $Model = new MoneyModel($this->getDoctrine());
        $account = $Model->getWithdrawAccount($uid);
        if (!$account) {
            $acc = "未绑定";
        }else{
            $acc = $account->getName()." - "."尾号***".substr($account->getAccount(),-4);
        }
        return $this->json_out(1,"数据获取成功",$acc);
    }
}
